package com.sctvcloud.renhe.ui.utils;

import com.jiongbull.jlog.JLog;
import com.sctvcloud.renhe.beans.Image;
import com.sctvcloud.renhe.beans.RssFeed;
import com.sctvcloud.renhe.beans.RssItem;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class RssHandler extends DefaultHandler {

    private RssFeed rssFeed;
    private RssItem rssItem;
    private Image image;

    private String lastElementName = "";// 标记变量，用于标记在解析过程中我们关心的几个标签，若不是我们关心的标签记做0

    private final int RSS_TITLE = 1;// 若是 title 标签，记做1，注意有两个title，但我们都保存在item的成员变量中
    private final int RSS_LINK = 2;// 若是 link 标签，记做2
    private final int RSS_AUTHOR = 3;
    private final int RSS_CATEGORY = 4;// 若是category标签,记做 4
    private final int RSS_PUBDATE = 5; // 若是pubdate标签,记做5,注意有两个pubdate,但我们都保存在item的pubdate成员变量中
    private final int RSS_COMMENTS = 6;
    private final int RSS_DESCRIPTION = 7;// 若是 description 标签，记做3

    private final int RSS_C_TITLE = 8;// 外部标题
    private final int RSS_C_IMAGE = 9;// 外部图片
    private final int RSS_C_DESCRIPTION = 10;// 外部描述
    private final int RSS_C_LINK = 11;// 外部链接
    private final int RSS_C_PUBDATE = 12;// 外部生成时间

    private final int RSS_I_TITLE = 13;// 图片标题
    private final int RSS_I_LINK = 14;// 图片链接
    private final int RSS_I_URL = 15;// 图片链接

    private int currentFlag = 0;

    private int currentType = 0;//0为外部， 1 为 image, 2 为item

    public RssHandler() {

    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
        rssFeed = new RssFeed();
        rssItem = new RssItem();
        image = new Image();
    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        super.characters(ch, start, length);
        // 获取字符串
        String text = new String(ch, start, length);
        JLog.i("i", "要获取的内容：" + text);

        switch (currentFlag) {
            case RSS_TITLE:
                rssItem.setTitle(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_PUBDATE:
                rssItem.setPubDate(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_LINK:
                rssItem.setLink(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_AUTHOR:
                rssItem.setAuthor(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_DESCRIPTION:
                rssItem.setDescription(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_C_TITLE:
                rssFeed.setTitle(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_C_IMAGE:
                image = new Image();
//                rssFeed.setImage(image);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_C_DESCRIPTION:
                rssFeed.setDescription(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_C_LINK:
                rssFeed.setLink(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_C_PUBDATE:
                rssFeed.setPubDate(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_I_TITLE:
                image.setTitle(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_I_URL:
                image.setUrl(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            case RSS_I_LINK:
                image.setLink(text);
                currentFlag = 0;// 设置完后，重置为开始状态
                break;
            default:
                break;
        }
    }

    @Override
    public void startElement(String uri, String localName, String qName,
                             Attributes attributes) throws SAXException {
        super.startElement(uri, localName, qName, attributes);
        if ("chanel".equals(localName)) {
            currentType = 0;
            // 这个标签内没有我们关心的内容，所以不作处理，currentFlag=0
            currentFlag = 0;
            return;
        }
        if ("image".equals(localName)) {
            currentType = 1;
            // 这个标签内没有我们关心的内容，所以不作处理，currentFlag=0
            currentFlag = 0;
            return;
        }

        if ("item".equals(localName)) {
            currentType = 2;
            rssItem = new RssItem();
            return;
        }
        if ("title".equals(localName)) {
            if (currentType == 0) {
                currentFlag = RSS_C_TITLE;
            } else if (currentType == 1) {
                currentFlag = RSS_I_TITLE;
            } else if (currentType == 2) {
                currentFlag = RSS_TITLE;
            }
            return;
        }
        if ("description".equals(localName)) {
            if (currentType == 0) {
                currentFlag = RSS_C_DESCRIPTION;
            } else if (currentType == 1) {
//                currentFlag = RSS_I_TITLE;
            } else if (currentType == 2) {
                currentFlag = RSS_DESCRIPTION;
            }
            return;
        }
        if ("link".equals(localName)) {
            if (currentType == 0) {
                currentFlag = RSS_C_LINK;
            } else if (currentType == 1) {
                currentFlag = RSS_I_LINK;
            } else if (currentType == 2) {
                currentFlag = RSS_LINK;
            }
            return;
        }
        if ("url".equals(localName)) {
            if (currentType == 0) {
            } else if (currentType == 1) {
                currentFlag = RSS_I_URL;
            } else if (currentType == 2) {
            }
            return;
        }
        if ("pubDate".equals(localName)) {
            if (currentType == 0) {
                currentFlag = RSS_C_PUBDATE;
            } else if (currentType == 1) {
            } else if (currentType == 2) {
                currentFlag = RSS_PUBDATE;
            }
            return;
        }
        if ("category".equals(localName)) {
            currentFlag = RSS_CATEGORY;
            return;
        }

        if ("author".equals(localName)) {
            currentFlag = RSS_AUTHOR;
            return;
        }

        if ("comments".equals(localName)) {
            currentFlag = RSS_COMMENTS;
            return;
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        super.endElement(uri, localName, qName);
        // 如果解析一个item节点结束，就将rssItem添加到rssFeed中。
        if ("item".equals(localName)) {
            currentType = 0;
            rssFeed.addItem(rssItem);
            return;
        } else if ("image".equalsIgnoreCase(localName)) {
            currentType = 0;
            rssFeed.setImage(image);
        }
    }

    @Override
    public void endDocument() throws SAXException {
        super.endDocument();
    }

    public RssFeed getRssFeed() {
        return rssFeed;
    }


}
